package com.appcan.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.persistence.EntityManager;
import javax.sql.DataSource;



public abstract class AbstractBaseBean {

	public static final String UNIT_NAME_BUSINESS = "businessUnit";
	public static final String UNIT_NAME_MSTREET = "mstreetUnit";
	public static final String UNIT_NAME_MERP = "merpUnit";
	public static final String UNIT_NAME_UFIDA = "ufidaUnit";
	
	abstract public EntityManager getManager();
	
	abstract public void setManager(EntityManager manager);
	public EntityManager getManager(Class c){
		return getManager();
	}
	
	@Resource
	SessionContext ctx;

	public DataSource getDataSource() {
		DataSource ds = (DataSource) ctx.lookup("java:/MStreetOracleDS");
		return ds;
	}

	public DataSource getDataSource(Long SysID) {
		DataSource ds = (DataSource) ctx.lookup("java:/MStreetOracleDS" + SysID);
		return ds;
	}

	public Connection getConnection() {
		try {
			DataSource ds = getDataSource();
			return ds.getConnection();
		} catch (Exception e) {
			RunTimeLogger.error(this, e.getMessage(), e);
		} 
		return null;
	}

	public Connection getConnection(Long sysID) throws BusinessException {
		Connection conn = null;
		try {
			DataSource ds = getDataSource(sysID);
			conn = ds.getConnection();
			if (conn == null) {
				throw new BusinessException(this, "无法获取sysID为�?" + sysID );
			}
		} catch (SQLException e) {
			RunTimeLogger.error(this, e.getMessage(), e);
			//throw new BusinessException(this, CommonMessage.NETWORK_BUSY);
		}
		return conn;
	}

	protected SessionContext getContext() {
		return ctx;
	}

	/**
	 * <p>
	 * ejb内部调用本地bean
	 * <p>
	 * 作�?：msf <br>
	 * 日期：Dec 20, 2006
	 * 
	 * @param sBeanName
	 * @return
	 */
	protected Object getBean(String sBeanName) {
		return ctx.lookup(sBeanName + "/local");
	}

	protected void close(Connection con, Statement stmt) {
		try {
			if (stmt != null) {
				stmt.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (Exception e) {
			RunTimeLogger.error(this, "无法关闭连接", e);
		}
	}

	protected void close(Connection con, PreparedStatement stmt) {
		try {
			if (stmt != null) {
				stmt.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (Exception e) {
			RunTimeLogger.error(this, "无法关闭连接", e);
		}
	}

	public void delShopItemInfo(Map map) throws BusinessException {
		// TODO Auto-generated method stub
		
	}
}